import 'package:dio/dio.dart';
import 'package:FiduMart/core/network/RequestClient.dart';
import 'package:FiduMart/core/network/model/base/base_model_entity.dart';
import 'package:FiduMart/core/network/model/club_detail_entity.dart';
import 'package:FiduMart/core/network/model/club_user_entity.dart';
import 'package:FiduMart/core/network/model/guild_flow_entity.dart';
import 'package:FiduMart/core/network/model/guild_state_entity.dart';
import 'package:FiduMart/core/network/model/image_path_entity.dart';
import 'package:FiduMart/core/network/model/invite_info_entity.dart';
import 'package:FiduMart/core/network/model/invite_list_entity.dart';
import 'package:FiduMart/core/network/model/invite_num_entity.dart';
import 'package:FiduMart/core/network/urls.dart';
import 'package:FiduMart/core/utils/crypto_util.dart';


class ApiService {
  static final ApiService _instance = ApiService._internal();
  final RequestClient _client = RequestClient();

  ApiService._internal();

  static ApiService get instance => _instance;

  /**
   * 获取邮箱验证码
   * @param email 邮箱
   * @param type 验证码类型 //1-注册 2-忘记密码 3-绑定"验证邮箱" 4-drn提现 5-绑定钱包地址 6-发起投资 7-购买宝箱 8-绑定邮箱
   */
  Future<BaseModelEntity> getEmailCode({
    required String email,
    required int type,
  }) async {
    var response = await _client.post(Urls.EMAIL_CODE, {
      "email": email,
      "type": type,
    });
    return BaseModelEntity.fromJson(response);
  }

  //忘记密码-重置
  Future<BaseModelEntity> forgetReset(
      {required String email,
      required String password,
      required String code}) async {
    var response = await RequestClient().post(Urls.FORGET_PASSWORD, {
      "email": email,
      "code": code,
      "password": CryptoUtil.generateMd5(password),
    },true);
    var responseEntity = BaseModelEntity.fromJson(response);
    return responseEntity;
  }

  //注册
  Future<BaseModelEntity> register(
      {required String email,
      required String password,
      required String code}) async {
    var response = await RequestClient().post(Urls.EMAIL_SIGN_UP, {
      "email": email,
      "code": code,
      "password": CryptoUtil.generateMd5(password),
      "autoLogin":1
      // todo 其他参数
    },true);
    var responseEntity = BaseModelEntity.fromJson(response);
    return responseEntity;
  }

  //获取公会成员列表
  Future<BaseModelEntity<ClubUserEntity>> getGuildMemberList({
    required String guildId,
    required int page,
    required int size,
    String? kw,
  }) async {
    var response = await _client.get(Urls.GUILD_MEMBER_LIST, {
      "guildId": guildId,
      "page": page,
      "pageSize": size,
      "kw": kw,
    });
    return BaseModelEntity<ClubUserEntity>.fromJson(response);
  }

  //申请列表
  Future<BaseModelEntity<ClubUserEntity>> getGuildApplyList({
    required String guildId,
    required int page,
    required int size,
    String? kw,
  }) async {
    var response = await _client.get(Urls.APPLY_LIST, {
      "guildId": guildId,
      "page": page,
      "pageSize": size,
      "kw": kw,
    });
    return BaseModelEntity<ClubUserEntity>.fromJson(response);
  }

  //公会详情
  Future<BaseModelEntity<ClubDetailEntity>> getGuildDetail({
    required String guildId,
  }) async {
    var response = await _client.get(Urls.CLUB_DETAIL, {
      "guildId": guildId,
    });
    return BaseModelEntity<ClubDetailEntity>.fromJson(response);
  }

  //上传图片 1-用户头像 2-公会头像 3-其他
  //id 公会ID
  Future<BaseModelEntity<ImagePathEntity>> uploadImage({
    required String filePath,
    required int type,
    String? id,
  }) async {
    var file = await MultipartFile.fromFile(filePath);
    var formData = FormData.fromMap({
      "file": file,
      "type": type,
      if (id != null) "id": id,
    });
    var response = await _client.postFile(Urls.UPLOAD_FILE, formData, true);
    return BaseModelEntity<ImagePathEntity>.fromJson(response);
  }

  //获取公会状态
  Future<BaseModelEntity<GuildStateEntity>> checkClubState() async {
    var response = await _client.post(Urls.CHECK_CLUB_STATE, {}, true);
    return BaseModelEntity<GuildStateEntity>.fromJson(response);
  }

  //创建公会
  Future<BaseModelEntity> createClub({
    required String name,
    required String avatar,
    required String introduce,
    String? featureTitle,
    String? featureContent,
    String? benefit,
    String? applyingReason,
    String? groupName,
    int? groupType,
    int? groupAddressType,
    String? groupAddress,
    String? contactUs,
    String? contactUsTg,
  }) async {
    var response = await _client.post(
        Urls.CREATE_CLUB,
        {
          "name": name,
          "avatar": avatar,
          "featureTitle": featureTitle,
          "featureContent": featureContent,
          "benefit": benefit,
          "introduce": introduce,
          "applyingReason": applyingReason,
          "groupName": groupName,
          "groupType": groupType,
          "groupAddressType": groupAddressType,
          "groupAddress": groupAddress,
          "contactUs": contactUs,
          "contactUsTg": contactUsTg,
        },
        true);
    return BaseModelEntity.fromJson(response);
  }

  //编辑公会
  Future<BaseModelEntity> editClub({
    required String name,
    required String avatar,
    required String introduce,
    String? featureTitle,
    String? featureContent,
    String? benefit,
    String? applyingReason,
    String? groupName,
    int? groupType,
    int? groupAddressType,
    String? groupAddress,
    String? contactUs,
    String? contactUsTg,
    String? guildId,
  }) async {
    var response = await _client.post(
        Urls.EDIT_CLUB,
        {
          "name": name,
          "avatar": avatar,
          "featureTitle": featureTitle,
          "featureContent": featureContent,
          "benefit": benefit,
          "introduce": introduce,
          "applyingReason": applyingReason,
          "groupName": groupName,
          "groupType": groupType,
          "groupAddressType": groupAddressType,
          "groupAddress": groupAddress,
          "contactUs": contactUs,
          "contactUsTg": contactUsTg,
          "guildId": guildId,
        },
        true);
    return BaseModelEntity.fromJson(response);
  }

  //申请加入公会
  Future<BaseModelEntity> applyJoin({
    required String guildId,
    String? reason,
  }) async {
    var response = await _client.post(Urls.APPLY_JOIN, {
      "guildId": guildId,
      "reason": reason,
    });
    return BaseModelEntity.fromJson(response);
  }

  

  //公会收支流水
  Future<BaseModelEntity<GuildFlowEntity>> getGuildFlowList({
    required int page,
    required int size,
    int? outIn,
    int? beginTime,
    int? endTime,
    int? gameType,
  }) async {
    var response = await _client.get(Urls.GET_GUILD_FLOW_LIST, {
      "page": page,
      "pageSize": size,
      "outIn": outIn,
      "beginTime": beginTime,
      "endTime": endTime,
      "gameType": gameType,
    });
    return BaseModelEntity<GuildFlowEntity>.fromJson(response);
  }

  //领取新人礼包
  Future<BaseModelEntity> getNewUserGift() async {
    var response = await _client.get(Urls.GET_NEW_USER_GIFT, {}, true);
    return BaseModelEntity.fromJson(response);
  }

  //邀请页面信息
  Future<BaseModelEntity<InviteInfoEntity>> getInviteInfo({
    required int gameType,
  }) async {
    var response = await _client.post(Urls.GET_INVITE_INFO, {
      "gameType": gameType,
    },true);
    return BaseModelEntity<InviteInfoEntity>.fromJson(response);
  }

  //邀请列表
  Future<BaseModelEntity<InviteListEntity>> getInviteList(
      {int? page, int? size, String? inviteCode}) async {
    var response = await _client.post(Urls.GET_INVITE_LIST, {
      "page": page,
      "pageSize": size,
      "inviteCode": inviteCode,
    },true);
    return BaseModelEntity<InviteListEntity>.fromJson(response);
  }

  //绑定游戏获取验证码
  Future<BaseModelEntity> getBindGameEmailCode({
    required String email,
    required int type,
  }) async {
    var response = await _client.post(Urls.GAME_EMAIL_CODE, {
      "email": email,
      "type": type,
    });
    return BaseModelEntity.fromJson(response);
  }

  Future<BaseModelEntity> getInviteNumInfo() async {
    var response = await _client.post(Urls.GET_INVITE_NUM, {});
    return BaseModelEntity<InviteNumEntity>.fromJson(response);
  }
}
